<template>
	<view class="u-wrap uni-pt-5">
		<view class="u-search-box">
			<view class="u-search-inner" @click="goSearch">
				<u-icon name="search" color="#ccc" :size="34"></u-icon>
				<text class="u-search-text">搜索商品</text>
			</view>
		</view>
		<view class="u-menu-wrap">
			<scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view" :scroll-top="scrollTop">
				<view v-for="(item,index) in tabbar" :key="index" class="u-tab-item"
					:class="[menuCurrent==index ? 'u-tab-item-active' : '']" :data-current="index"
					@tap.stop="swichMenu(index)">
					<text class="u-line-1">{{item.name}}</text>
				</view>
			</scroll-view>
			<view class="rightbar">
				<!-- <view class="screen">
					<view :class="['sitem',{'active':screenCurrent == 0}]" @click="screenChange(0)">综合</view>
					<view :class="['sitem',{'active':screenCurrent == 1}]" @click="screenChange(1)">优惠</view>
					<view :class="['sitem',{'active':screenCurrent == 2}]" @click="screenChange(2)">销量</view>
					<view :class="['sitem',{'active':screenCurrent == 3,'active1':screenCurrent == 4}]" @click="screenChange(3)">价格</view>
				</view> -->
				<scroll-view scroll-y class="right-scroll">
					<view class="goods_list">
						<view class="item" @click="navToDetailPage(item)" v-for="item in productList" :key="item.id">
							<view class="it" style="position: relative;overflow: hidden;">
								<image :src="item.img|imgfilter" mode="aspectFit"></image>
								
								<!-- <image v-if="item.num<0"  src="/static/img/ysq.png" style="position: absolute;z-index: 555;top:0;left: 0;"></image> -->
								<image v-if="item.num==0"  src="/static/img/ysq.png" style="position: absolute;z-index: 555;top:0;left: 0;"></image>
							</view>
							<view class="ct" >{{item.name}}</view>
							<view class="price">
								<view class="cp" >￥{{item.price|pricefilter}}</view>
								<view class="op" v-if="item.oldprice">￥{{item.oldprice}}</view>
								<view v-if="item.num>0" style="font-size: 10rpx;">已售<span style="font-size:30rpx;color:black;">{{item.sell_num}}</span>件</view>
								<view v-if="item.num<0">未发售</view>
								<view v-if="item.num==0">已售罄</view>
							</view>
						</view>
						<!--<view class="item">
							<view class="it">
								<image src="/static/img/product1.jpg" mode="aspectFill"></image>
							</view>
							<view class="ct">爱恩贝 内衣专享皂*5</view>
							<view class="price">
								<view class="cp">￥40</view>
								<view class="op">￥65</view>	
							</view>
						</view> -->
					</view>
				</scroll-view>
			</view>
		</view>
	</view>
</template>

<script>
	import classifyData from "@/common/classify.data.js";
	export default {
		data() {
			return {
				tabbar: classifyData,
				scrollTop: 0, //tab标题的滚动条位置
				menuCurrent: 0, // 预设当前项的值
				menuHeight: 0, // 左边菜单的高度
				menuItemHeight: 0, // 左边菜单item的高度
				screenCurrent: 0,
				productList: [],
			}
		},
		computed: {

		},
	 filters: {
			// 格式化物流信息
			pricefilter(str) {
				// var str=str+'66';
				var n=str.split(".");
				console.log(n)
				var pp="";
				if(n[1]!='00'){
					var p=n[1];
					pp=p.substr(0,1)
					return n[0]+"."+pp;
				}else{
					return n[0];
				}
			},
			imgfilter(str) {
				return str.replace("https","http")
			},
		},
		async onShow() {
			//
			var index = uni.getStorageSync('index');
			if(index){
				this.menuCurrent= index;
				uni.removeStorageSync('index');
			}
			
			var result = (await this.$ajax.get('Producttype/homelist.html'));
			this.tabbar = result.data.data
			
			console.log(this.tabbar);
			
			var result = (await this.$ajax.get('Product/getProductListById.html?id=' + this.tabbar[this.menuCurrent].id));
			this.productList = result.data.data.data
			
			//this.menuHeight = 100;
		},
		
		onReady() {
			// HBuilderX标准基座真机运行测试全屏视频广告位标识（adpid）为：1507000611
			// adpid: 1507000611 仅用于测试，发布时需要改为广告后台（https://uniad.dcloud.net.cn/）申请的 adpid
			// 广告后台申请的广告位(adpid)需要自定义基座/云打包/本地打包后生效
			this.adOption = {
				adpid: '__UNI__DCF580B'
			};
			// 创建广告实例
			//this.createFullScreenVideoAd();
		},
		onTabItemTap(e) {
			console.log("tabbar的点击事件监听" + e);
			this.music.playd('y899')
			
		},
		methods: {
			createFullScreenVideoAd() {
				var fullScreenVideoAd = this.fullScreenVideoAd = uni.createFullScreenVideoAd(this.adOption);
				fullScreenVideoAd.onLoad(() => {
					// 广告数据加载成功
					this.loading = false;
					console.log("onLoad");
				});
				fullScreenVideoAd.onClose((e) => {
					// 用户点击了关闭或返回键(仅Android有返回键)
					console.log("onClose " + e.isEnded);
				});
				fullScreenVideoAd.onError((err) => {
					console.log("onError", JSON.stringify(err));
					// 广告数据加载失败
					this.loading = false;
					uni.showToast({
						title: `${err.code} : ${err.errMsg}`
					})
			 });
			},
			showFullScreenVideoAd() {
				// 调用 fullScreenVideoAd.show()，如果数据正在加载中不会显示广告，加载成功后才显示
				// 在数据没有加载成功时，需要防止用户频繁点击显示广告
				if (this.loading == true) {
					return
				}
				this.loading = true;
				this.fullScreenVideoAd.show().then(() => {
					this.loading = false;
				}).catch((err) => {
					console.log(err.message);
					this.loading = false;
					uni.showToast({
						title: `${err.code} : ${err.errMsg}`
					})
				});
			},

			//详情
			navToDetailPage(item) {
				uni.navigateTo({
					url: `/pages/goods/goods?id=` + item.id
				});
			},
			goSearch() {
				uni.navigateTo({
					url: '/pages/goods/search'
				})
			},
			// 点击左边的栏目切换
			async swichMenu(index) {
				var result = (await this.$ajax.get('Product/getProductListById.html?id=' + this.tabbar[index].id));
				this.productList = result.data.data.data
				if (index == this.menuCurrent) return;
				this.menuCurrent = index;
				// 如果为0，意味着尚未初始化
				if (this.menuHeight == 0 || this.menuItemHeight == 0) {
					await this.getElRect('menu-scroll-view', 'menuHeight');
					await this.getElRect('u-tab-item', 'menuItemHeight');
				}
				// 将菜单菜单活动item垂直居中
				this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
			},
			// 获取一个目标元素的高度
			getElRect(elClass, dataVal) {
				new Promise((resolve, reject) => {
					const query = uni.createSelectorQuery().in(this);
					query.select('.' + elClass).fields({
						size: true
					}, res => {
						// 如果节点尚未生成，res值为null，循环调用执行
						if (!res) {
							setTimeout(() => {
								this.getElRect(elClass);
							}, 10);
							return;
						}
						this[dataVal] = res.height;
					}).exec();
				})
			},
			screenChange(index) {
				switch (index) {
					case 1:
						this.screenCurrent = 1
						break;
					case 2:
						this.screenCurrent = 2
						break;
					case 3:
						if (this.screenCurrent == 3) {
							this.screenCurrent = 4
						} else {
							this.screenCurrent = 3
						}
						break;
					case 4:
						if (this.screenCurrent == 4) {
							this.screenCurrent = 3
						} else {
							this.screenCurrent = 4
						}
						break;
					default:
						this.screenCurrent = 0
				}
			},
		}
	}
</script>

<style lang="scss">
	page{
		width:100%;
		height:100%;
		overflow:hidden;
	}
	.ct1 {
		width: 20%;
		font-size: 30rpx;
		color: #333;
		white-space: nowrap;
		text-overflow: ellipsis;
		overflow: hidden;
		margin-bottom: 5rpx;
	}
	.u-wrap {
		height: calc(100vh);
		/* #ifdef H5 */
		height: calc(100vh - var(--window-top));
		/* #endif */
		display: flex;
		flex-direction: column;
	}

	.u-search-box {
		padding: 0rpx 30rpx 20rpx 30rpx;
	}

	.u-menu-wrap {
		flex: 1;
		display: flex;
		overflow: hidden;
	}

	.u-search-inner {
		background-color: #eee;
		border-radius: 100rpx;
		display: flex;
		align-items: center;
		padding: 20rpx 30rpx;
	}

	.u-search-text {
		font-size: 26rpx;
		color: #aaa;
		margin-left: 10rpx;
	}

	.u-tab-view {
		width: 160rpx;
		height: 100%;
	}

	.u-tab-item {
		height: 10%;
		background: #f6f6f6;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 26rpx;
		color: #444;
		font-weight: 400;
		line-height: 1;
	}

	.u-tab-item-active {
		position: relative;
		color: #000;
		font-size: 30rpx;
		font-weight: 600;
		background: #fff;
	}

	.u-tab-item-active::before {
		content: "";
		position: absolute;
		border-left: 4px solid #fe124a;
		height: 32rpx;
		left: 0;
		top:43rpx;
	}

	.u-tab-view {
		height: 100%;
	}

	.rightbar {
		flex: 1;
		height: 100%;
		display: flex;
		flex-direction: column;
		justify-content: flex-start;
		align-items: center;
	}

	.screen {
		width: 100%;
		height: auto;
		overflow: hidden;
		background: #fff;
		padding: 40rpx 20rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;

		.sitem {
			height: 50rpx;
			line-height: 50rpx;
			background: #F1F1F1;
			border-radius: 100rpx;
			padding: 0 30rpx;
			font-size: 28rpx;
			color: #333;
			position: relative;

			&:nth-child(4) {
				padding: 0 35rpx 0 30rpx;
				text-indent: -10rpx;

				&::before {
					content: '';
					display: block;
					position: absolute;
					right: 15rpx;
					top: 17rpx;
					width: 0;
					height: 0;
					border-left: 3px solid transparent;
					border-right: 3px solid transparent;
					border-bottom: 4px solid #999;
				}

				&::after {
					content: '';
					display: block;
					position: absolute;
					right: 15rpx;
					top: 28rpx;
					width: 0;
					height: 0;
					border-left: 3px solid transparent;
					border-right: 3px solid transparent;
					border-top: 4px solid #999;
				}
			}

			&.active {
				color: #ff332f;

				&:nth-child(4) {
					&::before {
						border-left: 3px solid transparent;
						border-right: 3px solid transparent;
						border-bottom: 4px solid #ff332f;
					}

				}
			}

			&.active1 {
				color: #ff332f;

				&:nth-child(4) {
					&::after {
						border-left: 3px solid transparent;
						border-right: 3px solid transparent;
						border-top: 4px solid #ff332f;
					}

				}
			}
		}
	}

	.right-scroll {
		width: 100%;
		height: 100%;
		flex: 1;
	}

	.goods_list {
		height: auto;
		overflow: hidden;
		padding: 0rpx 30rpx 120rpx 30rpx;
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
		align-items: center;
		.item {
			width: 46%;
			// height: 420rpx;
			overflow: hidden;
			background: #fff;
			border-radius: 15rpx;
			margin: 15rpx 0 10rpx 0;
			.it {
				// width: 25%;
				// height: 25%;
				// overflow: hidden;
				margin-bottom: 15rpx;
				image {
					width: 242rpx;
					height: 242rpx;
				}
			}

			.ct {
				// height:40rpx;
				overflow: hidden;
				width:242rpx;
				font-size: 30rpx;
				line-height: 35rpx;
				// margin: 0 20rpx;
				color: #333;
				white-space: nowrap;
				text-overflow: ellipsis;
			
				
				
			}

			.price {
				width: 100%;
				display: flex;
				justify-content: flex-start;
				align-items: center;
				margin: 15rpx 0;

				.cp {
					font-size: 30rpx;
					color: #f14b90;
					font-weight: bold;
					margin-right: 10rpx;
				}

				.op {
					font-size: 26rpx;
					color: #999;
					text-decoration: line-through;
				}
			}
		}
	}
</style>
